﻿using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WCS.Infrastructure.ApllicationDto.Enums;

namespace Zocono.WCS.Domain.EquipmentLayoutManageDomain
{
    public class EquipmentLayoutService : IEquipmentLayoutService
    {
        private readonly ILogUtil _logUtil;
        private readonly IEquipmentLayoutRepository _EquipmentLayoutRepository;
        private readonly IMapper _Mapper;
        private readonly ISnowflake _Snowflake;


        public EquipmentLayoutService(ISnowflake snowflake, IEquipmentLayoutRepository EquipmentLayoutRepository, ILogUtil logUtil, IMapper mapper)
        {
            _EquipmentLayoutRepository = EquipmentLayoutRepository;
            _logUtil = logUtil;
            _Mapper = mapper;
            _Snowflake = snowflake;
        }

        public async Task<long> AddEquipmentLayoutAsync(EquipmentLayoutInfoDto EquipmentLayoutInfoDto)
        {
            var EquipmentLayoutInfo = _Mapper.Map<EquipmentLayoutInfoDto, EquipmentLayoutInfo>(EquipmentLayoutInfoDto);
            long id = _Snowflake.GetId();
            EquipmentLayoutInfo.ID = id;
            bool result = await _EquipmentLayoutRepository.InsertAsync(EquipmentLayoutInfo);
            return result ? id : 0;
        }

        public async Task<bool> DeleteEquipmentLayoutAsync(long ID)
        {
            bool result = await _EquipmentLayoutRepository.DeleteAsync(it => it.ID == ID);
            return result;
        }

        public async Task<bool> UpdateEquipmentLayoutAsync(EquipmentLayoutInfoDto EquipmentLayoutInfoDto)
        {
            var equipmentLayoutInfo = _Mapper.Map<EquipmentLayoutInfoDto, EquipmentLayoutInfo>(EquipmentLayoutInfoDto);
            bool result = await _EquipmentLayoutRepository.UpdateAsync(equipmentLayoutInfo);
            return result;
        }

        public async Task<PageDto<EquipmentLayoutInfoDto>> GetEquipmentLayoutAsync(QueryEquipmentLayoutInfoDto queryEquipmentLayoutInfoDto)
        {
            var pageModel = new PageModel() { PageIndex = queryEquipmentLayoutInfoDto.PageIndex, PageSize = queryEquipmentLayoutInfoDto.PageSize };
            var areaCode = queryEquipmentLayoutInfoDto.AreaCode;
            var defaultColor = queryEquipmentLayoutInfoDto.DefaultColor;
            var direction = queryEquipmentLayoutInfoDto.Direction;
            var equipmentLayoutCode = queryEquipmentLayoutInfoDto.EquipmentLayoutCode;

            var exp = Expressionable.Create<EquipmentLayoutInfo, DispatchEquipmentInfo, EquipmentLayoutTypeInfo>();
            exp.AndIF(!string.IsNullOrEmpty(areaCode), (a, b, c) => a.AreaCode.Contains(areaCode));
            exp.AndIF(!string.IsNullOrEmpty(defaultColor), (a, b, c) => a.DefaultColor == defaultColor);
            exp.AndIF(direction > -1, (a, b, c) => a.Direction == direction);
            exp.AndIF(!string.IsNullOrEmpty(equipmentLayoutCode), (a, b, c) => a.EquipmentLayoutCode.Contains(equipmentLayoutCode));
            var result = await _EquipmentLayoutRepository.GetEquipmentLayoutAsync(exp.ToExpression(), pageModel);
            if (result.Result.Any())
            {
                result.Result.ForEach(s => {
                    s.DirectionDescirption = ((EnumEquipmentDirection)Enum.Parse(typeof(EnumEquipmentDirection), s.Direction.ToString())).GetDescription(); 
                });
            }
            return result;
        }

        public async Task<EquipmentLayoutInfoDto> GetEquipmentLayoutByIDAsync(long ID)
        {
            var result = await _EquipmentLayoutRepository.GetByIdAsync(ID);
            var vModel = _Mapper.Map<EquipmentLayoutInfo, EquipmentLayoutInfoDto>(result);
            return vModel;
        }
    }
}
